package com.nmsl.statistic.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.utils.DateUtils;
import com.nmsl.medicine.domain.Medicine;
import com.nmsl.statistic.constants.Constants;
import com.nmsl.statistic.domain.*;
import com.nmsl.statistic.dto.MedicineDto;
import com.nmsl.statistic.mapper.RetailAttMapper;
import com.nmsl.statistic.mapper.RetailMedicineMapper;
import com.nmsl.statistic.mapper.RetailOrderMapper;
import com.nmsl.statistic.utils.RedisUtil;
import com.nmsl.system.exam_project.domain.ExaminationProject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.nmsl.statistic.mapper.TollManageMapper;
import com.nmsl.statistic.service.ITollManageService;

/**
 * 收费管理Service业务层处理
 *
 * @author mcslf
 * @date 2025-02-15
 */
@Service
public class TollManageServiceImpl implements ITollManageService {
    @Autowired
    private TollManageMapper tollManageMapper;

    @Autowired
    private RetailOrderMapper retailOrderMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private RetailMedicineMapper retailMedicineMapper;

    @Autowired
    private RetailAttMapper retailAttMapper;

    /**
     * 查询收费管理
     *
     * @param tollManageId 收费管理主键
     * @return 收费管理
     */
    @Override
    public TollManage selectTollManageByTollManageId(Long tollManageId) {
        return tollManageMapper.selectTollManageByTollManageId(tollManageId);
    }

    /**
     * 查询收费管理列表
     *
     * @param tollManage 收费管理
     * @return 收费管理
     */
    @Override
    public List<TollManage> selectTollManageList(TollManage tollManage) {
        return tollManageMapper.selectTollManageList(tollManage);
    }

    /**
     * 新增收费管理
     *
     * @param tollManage 收费管理
     * @return 结果
     */
    @Override
    public int insertTollManage(TollManage tollManage) {
        tollManage.setCreateTime(DateUtils.getNowDate());
        return tollManageMapper.insertTollManage(tollManage);
    }

    /**
     * 修改收费管理
     *
     * @param tollManage 收费管理
     * @return 结果
     */
    @Override
    public int updateTollManage(TollManage tollManage) {
        return tollManageMapper.updateTollManage(tollManage);
    }

    /**
     * 批量删除收费管理
     *
     * @param tollManageIds 需要删除的收费管理主键
     * @return 结果
     */
    @Override
    public int deleteTollManageByTollManageIds(Long[] tollManageIds) {
        return tollManageMapper.deleteTollManageByTollManageIds(tollManageIds);
    }

    /**
     * 删除收费管理信息
     *
     * @param tollManageId 收费管理主键
     * @return 结果
     */
    @Override
    public int deleteTollManageByTollManageId(Long tollManageId) {
        return tollManageMapper.deleteTollManageByTollManageId(tollManageId);
    }


    // 药品零售入口添加订单
    @Override
    public AjaxResult retialAdd(HashMap<String, Object> map) {

//        应付值
        String shoudPrice = (String) map.get("shoudPrice");
//        实付款值
        String realPrice = (String) map.get("realPrice");
        // 操作人员信息
        String opvaluestr = JSON.toJSONString(map.get("opvalue"));
//        患者信息

        String patientstr =JSON.toJSONString(map.get("patient"));
//        组装订单信息
        RetailOrder retailOrder = new RetailOrder();
//        操作人员信息组装
        OperInfo operInfo = JSONObject.parseObject(opvaluestr, OperInfo.class);
        System.out.println(operInfo);
        retailOrder.setOperName(operInfo.getUserName());
        retailOrder.setOperId(Long.valueOf(operInfo.getUserId()));
//        组装患者信息
        McslfPatient mcslfPatient = JSONObject.parseObject(patientstr, McslfPatient.class);
        retailOrder.setPatientAge(mcslfPatient.getAge());
        retailOrder.setPatientName(mcslfPatient.getRealName());
        retailOrder.setPatinetId(mcslfPatient.getPatientId().toString());
        retailOrder.setPatientPhone(mcslfPatient.getPhoneNumber());
        retailOrder.setRetailOrderStatus("1");
        retailOrder.setPayType("404");
        retailOrder.setOrederType("1");
        retailOrder.setShoudPrice(new BigDecimal(shoudPrice));
        retailOrder.setRealPrice(new BigDecimal(realPrice));
        retailOrder.setCreateTime(new Date());


//        ID 回显
                retailOrderMapper.insertRetailOrder(retailOrder);
// 解析药品信息
        List<Object> retailMedicine = (List<Object>) map.get("retailMedicine");
        List<MedicineDto> c = new ArrayList<>();
        retailMedicine.forEach(item->{
            String s = JSON.toJSONString(item);
            MedicineDto medicineDto = JSONObject.parseObject(s, MedicineDto.class);
            c.add(medicineDto);
        });


//        组装药品信息

        List<RetailMedicine> collect = c.stream().map(item -> {
            RetailMedicine retailMedicine1 = new RetailMedicine();
            retailMedicine1.setMedicineId(item.getMedicineId().longValue());
            retailMedicine1.setMedicineName(item.getMedicineName());
            retailMedicine1.setPurchasePrice(item.getPurchasePrice());
            retailMedicine1.setMedicinePrice(new BigDecimal(item.getRetailPrice().toString()));
            retailMedicine1.setMedicineNum(item.getMedicineNum());
            retailMedicine1.setMedicineAmunod(item.getMedicineAmunod());
            retailMedicine1.setRetailOrderId(retailOrder.getRetailOrderId());
            retailMedicine1.setCreateTime(retailOrder.getCreateTime());
            retailMedicine1.setRetailMedicineId(UUID.randomUUID().toString().replace("-", ""));
            return retailMedicine1;

        }).collect(Collectors.toList());
// 解析附加信息
        List<McslfExaminationProject> retailAtts = new ArrayList<>();
        List<Object> o = (List<Object>) map.get("retailAtt");
        o.forEach(item->{
            String s = JSON.toJSONString(item);
            McslfExaminationProject examinationProject = JSONObject.parseObject(s, McslfExaminationProject.class);
            System.out.println("++++++++++++++++++++++++++++++++++++++");
            System.out.println("++++++++++++++++++++++++++++++++++++++");
            System.out.println(examinationProject.toString());
            System.out.println("++++++++++++++++++++++++++++++++++++++");
            System.out.println("++++++++++++++++++++++++++++++++++++++");

            retailAtts.add(examinationProject);
        });

        List<RetailAtt> collectAtts = retailAtts.stream().map(item -> {
            String retailMedicineId = UUID.randomUUID().toString().replace("-", "");
            RetailAtt retailAtt = new RetailAtt();
            retailAtt.setAttId(item.getId());
            retailAtt.setRetailOrderId(retailOrder.getRetailOrderId());
            System.out.println("=====================================");
            System.out.println("=====================================");
            System.out.println(item.getAttNum());
            System.out.println("=====================================");
            System.out.println("=====================================");
            System.out.println("=====================================");
            System.out.println("=====================================");

            retailAtt.setAttNum(new BigDecimal(item.getAttNum()));
            retailAtt.setRetailAttId(retailMedicineId);
            retailAtt.setAttPrice(item.getRetailPrice());

            return retailAtt;
        }).collect(Collectors.toList());

//        附加信息持久化
     retailAttMapper.insertBatch(collectAtts);
      retailMedicineMapper.insertBatch(collect);
// 组装收费管理表信息
        TollManage tollManage = new TollManage();
        tollManage.setOrderType("1");
        tollManage.setOrderId(retailOrder.getRetailOrderId().toString());
        tollManage.setPatientName(retailOrder.getPatientName());
        tollManage.setPatientAge(retailOrder.getPatientAge());
        tollManage.setCreateTime(retailOrder.getCreateTime());
        tollManage.setShoudAmount(retailOrder.getShoudPrice());
        tollManage.setPayType("未支付");
        tollManage.setAmount(retailOrder.getRealPrice());
        tollManage.setStatus("0");
        tollManage.setDepartment("全科");
        tollManage.setPatientId(mcslfPatient.getPatientId().longValue());
        tollManage.setPatientPhone(retailOrder.getPatientPhone());
        tollManage.setDoctorName(retailOrder.getOperName());
        tollManage.setDockerId(Long.valueOf(operInfo.getUserId()));
//        信息持久化
        tollManageMapper.insertTollManage(tollManage);

//        TODO 排行


        return AjaxResult.success();
    }
}
